package com.jinxin.platform.cdfaceacs.task;


import com.jinxin.platform.cdfaceacs.bean.CdfaceacsDevice;
import com.jinxin.platform.cdfaceacs.pojo.domains.JurenAcsCommunityInfo;
import com.jinxin.platform.cdfaceacs.pojo.entity.Constant;
import com.jinxin.platform.cdfaceacs.redis.RedisUtil;
import com.jinxin.platform.cdfaceacs.service.IFaceAcsDeviceService;
import com.jinxin.platform.cdfaceacs.service.IFaceAcsRecordService;
import com.jinxin.platform.cdfaceacs.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * Description: bccah
 * Created by 34759 on 2019/8/16 15:57
 */
//@EnableScheduling
//@Configuration
//@Component
@Slf4j
public class FaceAcsScheduledTask {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IFaceAcsDeviceService faceAcsDeviceService;
    @Autowired
    private IFaceAcsRecordService faceAcsRecordService;

    /**
     * 每隔10分钟更新一次人脸记录
     */
    @Scheduled(cron = "0 0/10 * * * ? ")
    public void updateFaceAcsRecord(){
        long start = System.currentTimeMillis();
        List<CdfaceacsDevice> list = faceAcsDeviceService.list(new HashMap());
        for (CdfaceacsDevice deviceInfo : list) {
            String serialNum = deviceInfo.getSerialNum();
            String password = deviceInfo.getPassword();
            if(StringUtils.isEmpty(password) || StringUtils.isEmpty(serialNum)){continue;}
//            String startTime = "";
//            String key = Constant.MODULE_PREX + "latest_faceacs_record_time";
//            if(redisUtil.hasKey(key)){
//                startTime = (String) redisUtil.get(key);
//            }
//            startTime = StringUtils.isEmpty(startTime) ? DateUtil.getCurrentDayYMD()+"000000" : startTime;
//            startTime = startTime.replace("-","");
//            String endTime = DateUtil.transTimeToString(LocalDateTime.now(),"yyyyMMddHHmmss");
            // 设备那边现在不能根据时间来查询记录，只能全部查询所以时间都传0
            faceAcsRecordService.updateData(password,serialNum,"0","0");
        }
        System.out.println("人脸门禁出入记录同步耗时: " + (System.currentTimeMillis()-start)/1000/60);
    }

}
